package com.ruoyi.marry.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.marry.domain.TMarryImage;
import com.ruoyi.marry.mapper.TMarryImageMapper;
import com.ruoyi.marry.service.ITMarryImageService;
import com.ruoyi.oss.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 婚纱照Service业务层处理
 *
 * @author user_marry
 * @date 2020-11-19
 */
@Service
public class TMarryImageServiceImpl implements ITMarryImageService {
    @Autowired
    private TMarryImageMapper tMarryImageMapper;
    @Autowired
    private OssService ossService;

    /**
     * 查询婚纱照
     *
     * @param id 婚纱照ID
     * @return 婚纱照
     */
    @Override
    public TMarryImage selectTMarryImageById(Long id) {
        return tMarryImageMapper.selectTMarryImageById(id);
    }

    /**
     * 查询婚纱照列表
     *
     * @param tMarryImage 婚纱照
     * @return 婚纱照
     */
    @Override
    public List<TMarryImage> selectTMarryImageList(TMarryImage tMarryImage) {
        return tMarryImageMapper.selectTMarryImageList(tMarryImage);
    }

    /**
     * 新增婚纱照
     *
     * @param tMarryImage 婚纱照
     * @return 结果
     */
    @Override
    public int insertTMarryImage(TMarryImage tMarryImage) {
        //根据上传的图片地址保存成多条数据
        int count = 0;
        if (Constants.TYPE_001.equals(tMarryImage.getType()) && StringUtils.isNotNull(tMarryImage.getImageList()) && tMarryImage.getImageList().size() > 0) {
            for (int i = 0; i < tMarryImage.getImageList().size(); i++) {
                String url = tMarryImage.getImageList().get(i);
                tMarryImage.setImageUrl(url);
                tMarryImage.setSeq(tMarryImage.getSeq() + i);//排序序号累加
                tMarryImage.setCreateTime(DateUtils.getNowDate());
                count = tMarryImageMapper.insertTMarryImage(tMarryImage);
            }
        } else {
            tMarryImage.setCreateTime(DateUtils.getNowDate());
            count = tMarryImageMapper.insertTMarryImage(tMarryImage);
        }
        return count;
    }

    /**
     * 修改婚纱照
     *
     * @param tMarryImage 婚纱照
     * @return 结果
     */
    @Override
    public int updateTMarryImage(TMarryImage tMarryImage) {
        tMarryImage.setUpdateTime(DateUtils.getNowDate());
        return tMarryImageMapper.updateTMarryImage(tMarryImage);
    }

    /**
     * 删除婚纱照对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTMarryImageByIds(Long[] ids) {
        for (Long id : ids) {
            TMarryImage tMarryImage = tMarryImageMapper.selectTMarryImageById(id);
            int index = tMarryImage.getImageUrl().indexOf("marry/image");
            String fileKey = tMarryImage.getImageUrl().substring(index);
            ossService.delete(fileKey);
        }
        return tMarryImageMapper.deleteTMarryImageByIds(ids);
    }

    /**
     * 删除婚纱照信息
     *
     * @param id 婚纱照ID
     * @return 结果
     */
    @Override
    public int deleteTMarryImageById(Long id) {
        return tMarryImageMapper.deleteTMarryImageById(id);
    }

    /**
     * 删除照片by Type
     *
     * @param type 照片种类
     * @return 结果
     */
    public int deleteTMarryImageByType(String type) {
        return tMarryImageMapper.deleteTMarryImageByType(type);
    }
}
